from configparser import ConfigParser
import json
filename = 'test.ini'
cfg = ConfigParser()
read_ok = cfg.read(filename)

#将文件内容转换为字典
#d = {}
#for section,_ in cfg.items():
    
#     print(section)
#     print(cfg.items(section))
#     print("{}:{}".format(section,cfg.items(section) ))
#    d2 = {}
#    for m, n in cfg.items(section):
#        d2[m] = n
#    d[section] = d2

# 上面生成字典的代码改成 {字典生成式}

d = {section:{m:n for m, n in cfg.items(section)} for section,_ in cfg.items()}


print(d,end='\n\n\n')

print(json.dumps(d))


# 做的不错